<template>
  <div class="my-container">
    <!-- 未登录 -->
    <div class="header no-login" @click="$router.push('/login')" v-if="!showFlag">
      <!-- <img class="login-img" src="~@/assets/logo.jpg" alt="" /> -->
      <div class="login-img">
        <img class="logo-img" src="~@/assets/logo.jpg" />
      </div>
      <span class="text">登录/注册</span>
    </div>
    <!-- 已登录 -->
    <div class="header has-login" v-else>
      <!-- 基础信息 -->
      <div class="base">
        <div class="left">
          <van-image round class="avatar" fit="cover" :src="userInfo.photo" @click="previewPhoto" />
          <span class="name" v-text="userInfo.name"></span>
        </div>
        <div class="right">
          <van-button class="edit-user" size="mini" round to="/my-info">编&nbsp;辑</van-button>
        </div>
      </div>
      <!-- 宫格导航 -->
      <van-grid class="nav-grid mb-9" :column-num="3" clickable>
        <van-grid-item icon="shopping-cart-o" style="color: #1296db;" text="购物车">
        </van-grid-item>
        <van-grid-item icon="paid" text="钱包" style="color: #1afa29;" />
        <van-grid-item
          icon="balance-list-o"
          style="color: #f4ea2a;"
          text="优惠卷"
          @click="$router.push('/coupon')"
        />
      </van-grid>
      <!-- 单元格导航 -->
      <van-cell title="个人店铺" is-link to="/my-shoplist">
        <i slot="icon" class="iconfont icon-dianpu" style="color: #f4ea2a;"></i>
      </van-cell>
      <van-cell class="mb-9" title="修改密码" is-link @click="$router.push('/password')">
        <i slot="icon" class="iconfont icon-modify-password" style="color: #d81e06;"></i>
      </van-cell>
      <van-cell class="mb-9" title="我的收货地址" is-link @click="$router.push('/address')">
        <i slot="icon" class="iconfont icon-shouhuodizhi" style="color: #1296db;"></i>
      </van-cell>
      <van-cell class="mb-9" title="收藏店铺" is-link @click="$router.push('/shop')">
        <i slot="icon" class="iconfont icon-shoucang" style="color: #1afa29;"></i>
      </van-cell>
      <van-cell class="mb-9" title="进店纪录" is-link @click="$router.push('/record')">
        <i slot="icon" class="iconfont icon-history-alarm" style="color: #13227a;"></i>
      </van-cell>
      <van-cell
        class="login-out"
        title="退出登录"
        clickable
        @click="loginOut"
        v-if="userInfo.user_id"
      />
    </div>
  </div>
</template>

<script>
import { getUser } from '@/api/user'
import { ImagePreview } from 'vant'

export default {
  name: 'MyIndex',
  data() {
    return {
      userInfo: {},
      showFlag: false
    }
  },
  created() {
    if (this.$store.state.userToken) {
      this.showFlag = true
      this.getUserInfo()
    }
  },
  mounted() {},
  methods: {
    async getUserInfo() {
      const { data: res } = await getUser()
      if (res.meta.status === 200) {
        this.userInfo = res.data
      } else {
        this.$toast.fail(res.meta.msg)
      }
    },
    loginOut() {
      this.$dialog
        .confirm({
          title: '是否确认退出'
        })
        .then(() => {
          this.userInfo = {}
          this.showFlag = false
          this.$store.commit('setUserToken', null)
        })
        .catch(() => {})
    },
    // 头像预览
    previewPhoto() {
      ImagePreview({
        images: [this.userInfo.photo],
        closeable: true,
        showIndex: false
      })
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  .iconfont {
    font-size: 40px;
    margin-right: 20px;
  }
  .van-cell {
    font-size: 30px;
  }

  .header {
    height: 361px;
    // background: url('~@/assets/banner.png');
    background: url('~@/assets/banner.jpg');
    background-size: cover;
  }
  .no-login {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    .login-img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
      border-radius: 50%;
      overflow: hidden;
      padding: 10px;
      background-color: #fff;
      .logo-img {
        // width: 100%;
        height: 100%;
      }
    }
    .text {
      font-size: 40px;
      color: #fff;
    }
  }

  .has-login {
    .base {
      // height: 231px;
      height: 361px;
      padding: 76px 32px 23px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      .left {
        display: flex;
        justify-content: center;
        align-items: center;
        color: #fff;
        .avatar {
          width: 132px;
          height: 132px;
          border: 2px solid #fff;
        }
        .name {
          font-size: 40px;
          margin-left: 22px;
        }
      }
      .right {
        .edit-user {
          background-color: #222223;
          font-size: 30px;
          color: #fff;
          width: 120px;
        }
      }
    }
    .data {
      display: flex;
      .data-item {
        // 设置高度
        height: 130px;
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: #fff;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 15px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
      }
    }
  }
  .nav-grid {
    .grid-item {
      display: flex;
      justify-content: center;
      flex-direction: column;
      i.toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 28px;
      }
    }
  }
  .login-out {
    text-align: center;
    color: #ff744d;
  }
  .mb-9 {
    margin-bottom: 9px;
  }
}
</style>
